#!/usr/bin/env ruby

require File.join(File.dirname(__FILE__), "..", "config", "environment")

start_time = Time.now

puts "<html>"
puts "<head>"
puts "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
puts "<title>OpenStreetMap Statistics</title>"
puts "<style>th { text-align: left }</style>"
puts "</head>"
puts "<body>"
puts "<h2>OpenStreetMap stats report run at #{start_time}</h2>"

begin
  ActiveRecord::Base.transaction do
    user_count = User.where(:status => %w[active confirmed suspended]).count
    tracepoint_count = Tracepoint.count
    node_count = Node.where(:visible => true).count
    way_count = Way.where(:visible => true).count
    relation_count = Relation.where(:visible => true).count

    puts "<table>"
    puts "<tr><td>Number of users</td><td>#{user_count}</td></tr>"
    puts "<tr><td>Number of uploaded GPS points</td><td>#{tracepoint_count}</td></tr>"
    puts "<tr><td>Number of nodes</td><td>#{node_count}</td></tr>"
    puts "<tr><td>Number of ways</td><td>#{way_count}</td></tr>"
    puts "<tr><td>Number of relations</td><td>#{relation_count}</td></tr>"
    puts "</table>"

    puts "<h2>Top 50 users for uploads of GPS data</h2>"
    puts "<table>"
    puts "<tr><th>User</th><th>Number of Points</th></tr>"

    Trace.where(:inserted => true).group(:user_id).order("sum_size DESC").limit(50).sum(:size).each do |user, count|
      display_name = User.find(user).display_name.gsub("@", " at ").gsub(".", " dot ")
      puts "<tr><td><a href=\"/user/#{display_name}\">#{display_name}</a></td><td>#{count}</td></tr>"
    end

    puts "</table>"

    puts "<h2>Number of users editing over the past...</h2>"
    puts "<table>"
    puts "<tr><th>Data Type</th><th>Day</th><th>Week</th><th>Month</th></tr>"

    day_count = Trace.where("timestamp > NOW() - '1 DAY'::INTERVAL").distinct.count(:user_id)
    week_count = Trace.where("timestamp > NOW() - '7 DAYS'::INTERVAL").distinct.count(:user_id)
    month_count = Trace.where("timestamp > NOW() - '28 DAYS'::INTERVAL").distinct.count(:user_id)

    puts "<tr><th>GPX Files</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"

    day_count = OldNode.where("timestamp > NOW() - '1 DAY'::INTERVAL").joins(:changeset).distinct.count(:user_id)
    week_count = OldNode.where("timestamp > NOW() - '7 DAYS'::INTERVAL").joins(:changeset).distinct.count(:user_id)
    month_count = OldNode.where("timestamp > NOW() - '28 DAYS'::INTERVAL").joins(:changeset).distinct.count(:user_id)

    puts "<tr><th>Nodes</th><td>#{day_count}</td><td>#{week_count}</td><td>#{month_count}</td></tr>"

    puts "</table>"

    puts "<h2>Top users editing over the past...</h2>"
    puts "<table>"
    puts "<tr><th>Day</th><th>Week</th><th>Month</th></tr>"

    day_users = OldNode.where("timestamp > NOW() - '1 DAY'::INTERVAL").joins(:changeset).order("count_all DESC").group(:user_id).count
    week_users = OldNode.where("timestamp > NOW() - '7 DAYS'::INTERVAL").joins(:changeset).order("count_all DESC").limit(60).group(:user_id).count
    month_users = OldNode.where("timestamp > NOW() - '28 DAYS'::INTERVAL").joins(:changeset).order("count_all DESC").limit(60).group(:user_id).count

    day_users.zip(week_users, month_users).each do |row|
      puts "<tr>"
      row.each do |column|
        if column.nil?
          puts "<td></td>"
        else
          display_name = User.find(column[0]).display_name.gsub("@", " at ").gsub(".", " dot ")
          count = column[1]
          puts "<td>#{count} <a href=\"/user/#{display_name}\">#{display_name}</a></td>"
        end
      end
      puts "</tr>"
    end

    puts "</table>"
  end
rescue StandardError => e
  puts "<p><em>Exception: #{e}</em><br />#{e.backtrace.join('<br />')}</p>"
end

puts "<p>Report took #{Time.new - start_time} seconds to run</p>"
puts "</body>"
puts "</html>"

exit 0
